<template>
  <el-dialog
    class="add_dialog"
    width="600px"
    title="添加自定义分类"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="120px"
    >
      <el-form-item
        label="分类名称:"
        prop="cat_name"
      >
        <el-input
          placeholder="请输入分类名称"
          v-model="form.cat_name"
          clearable
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addShopCategory } from "@/api/shop";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        cat_name: ""
      },
      rules: {
        cat_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    // 提交
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          addShopCategory(this.form.cat_name).then(res => {
            this.dialogVisible = false;
            this.$emit("getList");
          })
        } else {
          this.$message({
            message: "请检查表单错误",
            type: "error"
          });
          return false;
        }
      });
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs['form'].resetFields();
          this.form = {
            cat_name: ''
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
